package io.gitee.sfch.chunmi.initializer.system;

import io.gitee.sfch.chunmi.controller.system.form.SystemUserAddForm;
import io.gitee.sfch.chunmi.service.system.SystemUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

/**
 * 超级用户初始化。超级用户是应用的第一个用户，默认用户；通过该用户来创建其他管理员用户
 */
@Slf4j
@Service
public class SuperUserInitializer implements ApplicationRunner {

    private static final String SUPER_USERNAME = "superAdmin";
    @Resource
    private SystemUserService systemUserService;

    @Override
    public void run(ApplicationArguments args) {
        if (systemUserService.hasUser(SUPER_USERNAME)) {
            return;
        }
        log.info("Initializing Super SystemUser");
        SystemUserAddForm addForm = new SystemUserAddForm();
        addForm.setUsername(SUPER_USERNAME);
        addForm.setPassword("123456");
        addForm.setName(SUPER_USERNAME);
        systemUserService.add(addForm);
    }
}
